<?php

namespace App\Lib;

use Aws\AwsClient;
use Aws\Credentials\Credentials;
use Aws\Signature\SignatureV4;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Str;
use Aws\S3\S3Client;


class DouYinWebSdk
{
    public $jd;

    public function __construct()
    {
//        $this->jd = new \Jd\Application('57495fee2587e4edf63b12bf92700577', '05a970437feb4cf498bd5c9d5ae0dcae');
        $this->client = new Client();

    }


    public function uploadVideo($file, $uploadId)
    {
        $fileCode = file_get_contents($file);
        $pre = crc32($fileCode);
        $cfc32 = dechex($pre);

        $headers = [
            'Referer' => 'https://dr.jd.com/',
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
            'Authorization' => 'SpaceKey/aweme/0/:version:v2:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTIxOTA3NzIsInNpZ25hdHVyZUluZm8iOnsiYWNjZXNzS2V5IjoiRFFRNklaMjNDTVZHUzI1VEpXNVUiLCJidWNrZXQiOiJ0b3MtYWxpbmMyb3NzLXYtMDAwMCIsImV4cGlyZSI6MTcxMjE5MDc3MiwiZmlsZUluZm9zIjpbeyJvaWRLZXkiOiJvVUliS2VPZkEybjlrblJKSXlSQTA2bzV3bERBRG1rTmdnQlpJQSIsImZpbGVUeXBlIjoiMCJ9XSwiZXh0cmEiOnsiYmxvY2tfbW9kZSI6IiIsImNvbnRlbnRfdHlwZV9ibG9jayI6IntcIm1pbWVfcGN0XCI6MCxcIm1vZGVcIjowLFwibWltZV9saXN0XCI6bnVsbCxcImNvbmZsaWN0X2Jsb2NrXCI6ZmFsc2V9IiwiZW5jcnlwdF9hbGdvIjoiIiwiZW5jcnlwdF9rZXkiOiIiLCJzcGFjZSI6ImF3ZW1lIiwidG9zX21ldGEiOiJ7XCJVU0VSX0lEXCI6XCI2NzU2NzI4NzU5OFwifSJ9fX0.pQglkQ6EQbRCLwCVgsv8rZwlkBm6859UG1mCd8QGdl8',
            'Content-Crc32' => $cfc32
        ];
        $options = [
            "query" => [
                "uploadid" => $uploadId,
                "part_number" => 1,
                "phase" => "transfer",
                "part_offset" => 0
            ],
            "body" => $fileCode,
            "verify" => false
        ];
        $request = new Request('POST', "https://edge-alinc2-oss-slb1.bytecdn.cn/upload/v1/tos-alinc2oss-v-0000/oUIbKeOfA2n9knRJIyRA06o5wlDADmkNggBZIA", $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        return json_decode($res->getBody(), true);
    }

    public function getVideo()
    {

        $headers = [
            'Referer' => 'https://dr.jd.com/',
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        ];
        $options = [
            "query" => [
                "video_id" => "v0d00fg10000co6a7d3c77u8ug3j74sg",
//                "part_number" => 1,
//                "phase" => "transfer",
//                "part_offset" => 0
            ],
//            "body" => $fileCode,
            "verify" => false
        ];
        $request = new Request('POST', "https://creator.douyin.com/web/api/media/video/transend", $headers);
        $res = $this->client->sendAsync($request, $options)->wait();
        return json_decode($res->getBody(), true);

    }

//    public function getSin(key, date_stamp, regionName, serviceName){
//
//    }


//    public function getSign($sk,$date,$region,$service){
//
//        $tmp = hash('sha256',"AWS4{$sk}" . $date,true);
//        dd($tmp);
//
//    }

    public function getSigningKey($shortDate, $region, $service, $secretKey)
    {
        $dateKey = hash_hmac(
            'sha256',
            $shortDate,
            "AWS4{$secretKey}",
            true
        );
        $regionKey = hash_hmac('sha256', $region, $dateKey, true);
        $serviceKey = hash_hmac('sha256', $service, $regionKey, true);

        $res = hash_hmac(
            'sha256',
            'aws4_request',
            $serviceKey,
            true
        );

        return $res;
    }

    public function randomStr()
    {
        return Str::random(11);
    }

    public function getQueryStr($param)
    {
        $res = "";
        foreach ($param as $key => $item) {
            $res .= "{$key}={$item}&";
        }
        return rtrim($res, '&');
    }


    public function getAuth($key, $token)
    {

        $str = "AWS4-HMAC-SHA256 Credential={$key}/20240403/cn-north-1/imagex/aws4_request, SignedHeaders=x-amz-date;x-amz-security-token";

    }

    public function generateAuthorization($secretKey, $region, $serviceName, $queryStr, $ldt, $sessionToken, $sdt, $accessKeyId)
    {

        $signKey = $this->getSigningKey($sdt, $region, $serviceName, $secretKey);

        $canonical_headers = "x-amz-date:{$ldt}\nx-amz-security-token:{$sessionToken}\n";
        $canonical_request = "GET\n/\n{$queryStr}\n{$canonical_headers}\nx-amz-date;x-amz-security-token\n" . hash("sha256", "", true);
        $algorithm = 'AWS4-HMAC-SHA256';
        $credential_scope = "{$sdt}/{$region}/{$serviceName}/aws4_request";

        $toSign = "{$algorithm}\n{$ldt}\n{$credential_scope}\n" . hash("sha256", $canonical_request, true);
        $signature = hash_hmac('sha256', $toSign, $signKey);
        $data = "AWS4-HMAC-SHA256 Credential={$accessKeyId}/{$sdt}/{$region}/{$serviceName}/aws4_request, SignedHeaders=x-amz-date;x-amz-security-token, Signature={$signature}";
        return $data;
    }


    public function sdkTest(){

//        $s3 = new S3Client([
//            'version' => 'latest',
//            'region'  => 'cn-north-1'
//        ]);

//        $s3->

//        $s4 = new SignatureV4('vod','cn-north-1');
//        $request = new Request('GET','https://vod.bytedanceapi.com',[],['query'=>[
//           'Action'
//        ]]);
//        $credit = new Credentials('AKTPNjkwM2Y2NGJjNDQ1NDRkN2JjZGIyYzExMzIzZGY5Y2I','RdZsETGbwzLcqW7EIE9gmzfj6RzCdN+1/agW998i6W9Oa6CEvt436IKZyzElaGMH','STS2eyJMVEFjY2Vzc0tleUlEIjoiQUtMVFlqZGpZVFV3WldRd1pEVTBORFJtTW1Fd05XVTVObUUxTVRkaVl6VXlaVGciLCJBY2Nlc3NLZXlJRCI6IkFLVFBOamt3TTJZMk5HSmpORFExTkRSa04ySmpaR0l5WXpFeE16SXpaR1k1WTJJIiwiU2lnbmVkU2VjcmV0QWNjZXNzS2V5IjoibUkwL2dFb25oaWEwUUJPQjEwcmdYbk1aMittOUVVUTQ4MGZjUnZCOGgxSE9RYXJZU3RaOXozdlNibDVmdm1JcEFJM0U2bDAwbTlhZUdTcThNVXRMaEdXbll2QnRtNzc4eWRJU29najFWT0U9IiwiRXhwaXJlZFRpbWUiOjE3MTI1ODcwODEsIlBvbGljeVN0cmluZyI6IntcIlN0YXRlbWVudFwiOlt7XCJFZmZlY3RcIjpcIkFsbG93XCIsXCJBY3Rpb25cIjpbXCJ2b2Q6KlwiLFwiSW1hZ2VYOipcIl0sXCJSZXNvdXJjZVwiOltcIipcIl19LHtcIkVmZmVjdFwiOlwiQWxsb3dcIixcIkFjdGlvblwiOltcIlVzZXJJZFwiXSxcIlJlc291cmNlXCI6W1wiMzU1ODc1NjY0MTc0MjYzXCJdfSx7XCJFZmZlY3RcIjpcIkFsbG93XCIsXCJBY3Rpb25cIjpbXCJBcHBJZFwiXSxcIlJlc291cmNlXCI6W1wiMTEyOFwiXX0se1wiRWZmZWN0XCI6XCJBbGxvd1wiLFwiQWN0aW9uXCI6W1wiVXNlclJlZmVyZW5jZVwiXSxcIlJlc291cmNlXCI6W1wie1xcXCJwc21cXFwiOlxcXCJkb3V5aW4uY3JlYXRvci5jb250ZW50XFxcIn1cIl19LHtcIkVmZmVjdFwiOlwiQWxsb3dcIixcIkFjdGlvblwiOltcIlNlc3Npb25DaGVja1wiXSxcIlJlc291cmNlXCI6W1wiVWlkXCJdfSx7XCJFZmZlY3RcIjpcIkFsbG93XCIsXCJBY3Rpb25cIjpbXCJQU01cIl0sXCJSZXNvdXJjZVwiOltcImRvdXlpbi5jcmVhdG9yLmNvbnRlbnRcIl19XX0iLCJTaWduYXR1cmUiOiJiNDlhN2U1OGNlYmUxZTdkNzdlNTBmZTNmZjVhYjNmZjlkYTVjOTg1OGNkZjg4MWE3MTg5OTA3ZDdmNTI0MmI5In0=');
//        $res = $s4->signRequest($request,$credit);
//        dd($res);
        $config = [
//            'Api' => 'ApplyUploadInner',
            'version' => '2020-11-19',
            'region' => 'cn-north-1',
            'service' => 'vod',
            'credentials' => new Credentials('AKTPNjkwM2Y2NGJjNDQ1NDRkN2JjZGIyYzExMzIzZGY5Y2I','RdZsETGbwzLcqW7EIE9gmzfj6RzCdN+1/agW998i6W9Oa6CEvt436IKZyzElaGMH','STS2eyJMVEFjY2Vzc0tleUlEIjoiQUtMVFlqZGpZVFV3WldRd1pEVTBORFJtTW1Fd05XVTVObUUxTVRkaVl6VXlaVGciLCJBY2Nlc3NLZXlJRCI6IkFLVFBOamt3TTJZMk5HSmpORFExTkRSa04ySmpaR0l5WXpFeE16SXpaR1k1WTJJIiwiU2lnbmVkU2VjcmV0QWNjZXNzS2V5IjoibUkwL2dFb25oaWEwUUJPQjEwcmdYbk1aMittOUVVUTQ4MGZjUnZCOGgxSE9RYXJZU3RaOXozdlNibDVmdm1JcEFJM0U2bDAwbTlhZUdTcThNVXRMaEdXbll2QnRtNzc4eWRJU29najFWT0U9IiwiRXhwaXJlZFRpbWUiOjE3MTI1ODcwODEsIlBvbGljeVN0cmluZyI6IntcIlN0YXRlbWVudFwiOlt7XCJFZmZlY3RcIjpcIkFsbG93XCIsXCJBY3Rpb25cIjpbXCJ2b2Q6KlwiLFwiSW1hZ2VYOipcIl0sXCJSZXNvdXJjZVwiOltcIipcIl19LHtcIkVmZmVjdFwiOlwiQWxsb3dcIixcIkFjdGlvblwiOltcIlVzZXJJZFwiXSxcIlJlc291cmNlXCI6W1wiMzU1ODc1NjY0MTc0MjYzXCJdfSx7XCJFZmZlY3RcIjpcIkFsbG93XCIsXCJBY3Rpb25cIjpbXCJBcHBJZFwiXSxcIlJlc291cmNlXCI6W1wiMTEyOFwiXX0se1wiRWZmZWN0XCI6XCJBbGxvd1wiLFwiQWN0aW9uXCI6W1wiVXNlclJlZmVyZW5jZVwiXSxcIlJlc291cmNlXCI6W1wie1xcXCJwc21cXFwiOlxcXCJkb3V5aW4uY3JlYXRvci5jb250ZW50XFxcIn1cIl19LHtcIkVmZmVjdFwiOlwiQWxsb3dcIixcIkFjdGlvblwiOltcIlNlc3Npb25DaGVja1wiXSxcIlJlc291cmNlXCI6W1wiVWlkXCJdfSx7XCJFZmZlY3RcIjpcIkFsbG93XCIsXCJBY3Rpb25cIjpbXCJQU01cIl0sXCJSZXNvdXJjZVwiOltcImRvdXlpbi5jcmVhdG9yLmNvbnRlbnRcIl19XX0iLCJTaWduYXR1cmUiOiJiNDlhN2U1OGNlYmUxZTdkNzdlNTBmZTNmZjVhYjNmZjlkYTVjOTg1OGNkZjg4MWE3MTg5OTA3ZDdmNTI0MmI5In0=')
        ];
        $service = new AwsClient($config);
        $res = $service->getCredentials();
        dd($res);
    }

}
